package cn.hy.face.utils;
import cn.hy.face.common.enums.ResultCode;
import java.io.Serializable;

/**
 * @program: face
 * @author: qingxiangen
 * @create: 2019-09-04 17:53
 * @description: 响应结果类
 **/
public class Result<T> implements Serializable {
    private static final long serialVersionUID = 1175493446574743561L;
    /**
     * 响应code 200：成功，其他：异常
     */
    private Integer code;
    /**
     * 响应消息
     */
    private String message;
    /**
     * 响应结果集
     */
    private T result;

    public Result() {
    }

    public Result(Integer code, String message, T result) {
        this.code = code;
        this.message = message;
        this.result = result;
    }

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    /**
     * 服务器请求成功返回结果集
     * @param data
     * @return
     */
    public static<T> Result<T> success(T data) {
        return success(ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 服务器请求成功返回结果集
     * @param message
     * @param data
     * @return
     */
    public static<T> Result<T> success(String message, T data) {
        return new Result(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 参数异常
     * @param
     * @return
     */
    public static<T> Result<T> paramError(String message) {
        return new Result(ResultCode.PARAM_ERROR.getCode(), message, null);
    }

    /**
     * 服务器请求失败返回结果集
     * @param data
     * @return
     */
    public static<T> Result<T> error(T data) {
        return error(ResultCode.SYS_ERROR.getMessage(), data);
    }

    /**
     * 服务器请求失败返回结果集
     * @param message
     * @param data
     * @return
     */
    public static<T> Result<T> error(String message, T data) {
        return new Result(ResultCode.SYS_ERROR.getCode(), message, data);
    }
}
